import { _decorator, Component, sp } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('AnimationController')
export class AnimationController extends Component {

    private curAnimName: string = "";
    private curLoop: boolean = false;
    private isComplete: boolean = true;
    private spineAnim: sp.Skeleton;

    public completeListener: any;

    onLoad() {
        this.isComplete = true;
        this.spineAnim = this.node.getComponent(sp.Skeleton);
    }

    SetNone() {
        this.curAnimName = "";
    }

    UpdateAnim(animName: string, loop: boolean, track: number = 0) {
        if (loop) {
            if (this.curAnimName == animName) {

            }
            else {
                // if (this.isComplete) 
                {
                    this.spineAnim.timeScale = 1;
                    this.curAnimName = animName;
                    // console.log("animName :" + animName);
                    this.spineAnim.setSkin(animName);
                    this.spineAnim.setAnimation(track, animName, loop);
                }
            }
        }
        else {
            // if (this.isComplete)
            {
                // console.log("animName :" + animName);
                if (animName == "Attack2") {
                    this.spineAnim.timeScale = 2;
                }
                this.spineAnim.setSkin(animName);
                this.spineAnim.setAnimation(track, animName, loop);
                this.curLoop = loop;
                this.curAnimName = animName;
                // 监听动画结束事件（可选）
                this.spineAnim.setCompleteListener((trackEntry) => {
                    // console.log("Animation complete:", trackEntry.animation.name);
                    this.isComplete = true;
                    if (this.completeListener) {
                        this.completeListener();
                    }
                });
                this.isComplete = false;
            }
        }
    }

    SetIdle(animName) {
        this.spineAnim.setSkin(animName);
        this.spineAnim.setAnimation(0, animName, true);
    }

}


